import pandas as pd
import time
from utils.sult import suit

_sult = suit.keys()


class Analysis:
    # Todo Group 值传入？？？
    def __init__(self):
        self.df = pd.DataFrame(columns=_sult, index=['2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K', 'A'])
        self.df['spade']['3'] = 1
        # self.df['spade']['4'] = 1
        # self.df['Heart']['4'] = 1
        # self.df['Diamond']['4'] = 1
        # self.df['Club']['4'] = 1
        # self.df['spade']['5'] = 1
        self.df['spade']['6'] = 1
        self.df['spade']['7'] = 1

    @property
    def mark(self):
        if self.checkTongHuashun():
            return 8
        elif self.checkSiTiao():
            return 7
        elif self.checkHuLu():
            return 6
        elif self.checkTonghua():
            return 5
        elif self.checkShunZi():
            return 4
        elif self.checkSanTiao():
            return 3
        elif self.checkLiangDui():
            return 2
        elif self.checkYidui():
            return 1
        else:
            return 0

    def checkTongHuashun(self):
        "是否同花顺"
        #  检查同花花色是不是顺子
        default = False
        result = default
        if self.checkTonghua() and self.checkShunZi():

            c = (self.df.agg('sum', axis=0))
            tongHua = c[c >= 5].index[0]
            a1 = self.df[tongHua]
            a1.index = [2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]
            for i in range(len(a1) - 4):
                i += 2  # 牌号是从2开始的
                if (
                        a1[i + 0] > 0 and
                        a1[i + 1] > 0 and
                        a1[i + 2] > 0 and
                        a1[i + 3] > 0 and
                        a1[i + 4] > 0
                ):
                    result = not default
                    break
        return result

    def checkSiTiao(self):
        tb = self.get_agg_align()
        return bool(len(tb[tb == 4]))

    def checkHuLu(self):
        tb = self.get_agg_align()
        return bool(len(tb[tb == 3])) and bool(len(tb[tb == 2]))

    def checkTonghua(self):
        "是否同花"
        return self.df.agg('sum', axis=0).agg('max') >= 5

    def checkShunZi(self):
        "是否顺子"
        default = False
        result = default
        a1 = (self.df.agg('sum', axis=1))
        a1.index = [2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]
        for i in range(len(a1) - 4):
            i += 2  # 牌号是从2开始的
            if (
                    a1[i + 0] > 0 and
                    a1[i + 1] > 0 and
                    a1[i + 2] > 0 and
                    a1[i + 3] > 0 and
                    a1[i + 4] > 0
            ):
                result = not default
                break
        return result

    def checkSanTiao(self):
        tb = self.get_agg_align()
        return bool(len(tb[tb == 3]))

    def checkLiangDui(self):
        tb: list = self.get_agg_align().to_list()
        return tb.count(2) >= 2

    def checkYidui(self):
        tb: list = self.get_agg_align().to_list()
        return tb.count(2) == 1

    def get_agg_align(self):
        a1 = (self.df.agg('sum', axis=1))
        a1.index = [2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]
        a1_exist = a1[a1 > 0]
        a1_exist.sort_values(ascending=False, inplace=True)
        return a1_exist


if __name__ == '__main__':
    s = time.time()
    a = Analysis()
    print(time.time()-s)